//
//  StrategyViewController.m
//  XhlTrip
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "StrategyViewController.h"
#import "CusNaviView.h"
#import "StrategyDetailController.h"
#import "LeaonWorking.h"
#import "StrategyCollectionCell.h"
#import "StrategyModel.h"
#import "MBProgressHUD.h"
@interface StrategyViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *allArr;

@end

@implementation StrategyViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.allArr = [NSMutableArray array];
       
        [self handData];
        
    }
    return self;
}

#pragma mark - 网络请求处理数据
- (void)handData
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:hud];
    hud.labelText = @"正在加载";
    [hud show:YES];
    
    [LeaonWorking netWorkingHanderGetGataWithUrl:@"https://chanyouji.com/api/destinations.json" pathname:@"strategy"resultBlock:^(id result) {
        [self.allArr addObjectsFromArray:result];
        [self.collectionView reloadData];
        [hud removeFromSuperview];
        [hud release];

      
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
   

    self.tabBarController.tabBar.hidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    /* 创建collectionview */
    [self createCollectionView];
}
- (UIImage *)nameWithPath:(NSString *)paths
{
    NSString *path = [[NSBundle mainBundle] pathForResource:paths ofType:nil];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    return image;
}


- (void)changeNaviBar
{
#pragma mark - 头部标题栏
   // self.view.backgroundColor = [UIColor redColor];
    self.navigationController.navigationBar.translucent = NO;
    CusNaviView *titleview = [[CusNaviView alloc] initWithFrame:CGRectMake(10 * Width, 0, self.navigationController.navigationBar.frame.size.width - 20 * Width, self.navigationController.navigationBar.frame.size.height)];

    self.navigationItem.titleView = titleview;
    [titleview release];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"view.png"]forBarMetrics:UIBarMetricsDefault];
    
}


- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 间距
    layout.minimumInteritemSpacing = 5;
    
    //定义每个UICollectionView 的 margin section与section之间的距离
    // UIEdgeInsetsMake(top,left,botton,right);
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    layout.headerReferenceSize = CGSizeMake(0, 50);
    
    // 滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    [self.view addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView release];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    [_collectionView registerClass:[StrategyCollectionCell class] forCellWithReuseIdentifier:@"rr"];
    
    // 注册sectionheader的重用池
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"tt"];

    [_collectionView release];
}

/* 分区数 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.allArr.count;
}


/* 设定每一个item大小 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGSize ff = CGSizeMake(172 * Width, 223 * Height);
    return ff;
}


/* cell的个数 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *arr = [self.allArr[section] objectForKey:@"destinations"];
    return arr.count;
}


/* collection的cell */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"rr" forIndexPath:indexPath];
    
    
    NSArray *arr = [self.allArr[indexPath.section] objectForKey:@"destinations"];
    NSDictionary *dic = arr[indexPath.item];
    
#pragma mark - 给model赋值
    StrategyModel *model = [[[StrategyModel alloc] init] autorelease];
    [model setValuesForKeysWithDictionary:dic];
    cell.strategyModel = model;
    return cell;
}

/* 选中cell进入下一页*/
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyDetailController *detail = [[StrategyDetailController alloc] init];
    NSArray *arr = [self.allArr[indexPath.section] objectForKey:@"destinations"];
    NSDictionary *dic = arr[indexPath.item];
    [detail passValue:[[dic objectForKey:@"id"] stringValue]];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"攻略";
    backItem.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = backItem;
#pragma mark - 返回键是白色
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
   
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        // sectionheader的设定
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"tt" forIndexPath:indexPath];
        
        while ([header.subviews lastObject] != nil) {
            [[header.subviews lastObject] removeFromSuperview];  //删除并进行重新分配
        }

        NSArray *arr = [NSArray arrayWithObjects:@"国外 · 亚洲", @"国外 · 欧洲", @"美洲、大洋洲、非洲与南极洲", @"国内 · 港澳台", @"国内 · 大陆", nil];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10 * Width, 0, header.frame.size.width - 10 * Width, header.frame.size.height)];
        label.font = [UIFont fontWithName:@"Verdana- Italic" size:18];

        label.text = arr[indexPath.section];
        [header addSubview:label];
        [label release];
        
        return header;
    }
    return nil;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self changeNaviBar];
    self.tabBarController.tabBar.hidden = NO;
    
}

- (void)dealloc
{
    [_allArr release];
    [_collectionView release];
    [super dealloc];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
